﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using WP7Client.Helpers;

namespace WP7Client.Managers
{
    /// <summary>
    /// De refresh manager zorgt ervoor dat alle controller om een bepaalde interval geupdate worden.
    /// </summary>
    public static class RefreshManager
    {
        private static Timer timer;
        private static Thread refreshThread;

        /// <summary>
        /// Start de refresh manager
        /// </summary>
        public static void Start()
        {
            refreshThread = new Thread(Run);
            refreshThread.Start();
        }

        /// <summary>
        /// Stop de refreshmanager
        /// </summary>
        public static void Stop()
        {
            refreshThread = null;
            timer = null;
        }

        /// <summary>
        /// Run de thread en start dus de timer
        /// </summary>
        private static void Run()
        {
            timer = new Timer(new TimerCallback(timerFired), null, 0, 30000);
        }

        /// <summary>
        /// Update de controller als de timer afvuurt
        /// </summary>
        /// <param name="state"></param>
        private static void timerFired(object state)
        {
            ControllerHelper.Update();
        }

        /// <summary>
        /// Veranderd de tijd interval
        /// </summary>
        /// <param name="time">Tijd in milliseconden</param>
        public static void ChangeTimerInterval(Int32 time)
        {
            timer.Change(0, time);
        }
    }
}
